//
//  StampInputView.m
//  ENTBoostChat
//
//  Created by zhong zf on 14-9-17.
//  Copyright (c) 2014年 EB. All rights reserved.
//

#import "StampInputView.h"
#import "ENTBoostChat.h"
#import "ENTBoost.h"
#import "SETextView.h"
#import "UIImage+UIImageScale.h"

@implementation StampButton

@end

@implementation StampInputView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.isRealShow = NO;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)fillStamps
{
    NSMutableArray* arry = [NSMutableArray array];
    if (!self.isRealShow) {
        //清理旧的图标视图
        for (UIView* view in self.subviews) {
            [view removeFromSuperview];
        }
        
        ENTBoostKit* ebKit = [ENTBoostKit sharedToolKit];
        if (ebKit.isEmotionLoaded)
            self.isRealShow = YES;
        
        //显示新的图标视图
        NSArray* expressions = ebKit.expressions;
        if (expressions) {
            const int maxCountOfRow = 8;
            const int maxRow = 5;
            for (int i =0;i < expressions.count; i++) {
                if ((i/maxCountOfRow) >= maxRow) //最多只显示maxRow行，以后考虑分页
                    break;
                
                EBEmotion* emotion = [expressions objectAtIndex:i];
                
                StampButton* button = [StampButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(7 + 39*(i%maxCountOfRow), 7 + (i/maxCountOfRow)*(32 + 7), 32, 32);
                button.backgroundColor = [UIColor clearColor];
                
//                //设置背景图
                [button setImage:[UIImage imageWithContentsOfFile:emotion.dynamicFilepath]forState:UIControlStateNormal];
//                UIWebView* view = [[UIWebView alloc] initWithFrame:button.bounds];
//                [view loadData:[NSData dataWithContentsOfFile:emotion.dynamicFilepath] MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
//                [button addSubview:view];
                
                //绑定自定义数据
                button.data = @{STAMP_CUSTOM_DATA_EMOTION_NAME: emotion};
                
                //绑定点击事件
                [button addTarget:self action:@selector(stampTap:) forControlEvents:UIControlEventTouchUpInside];

                [arry addObject:button];
            }
        }
//        self.imageButtons =  [NSArray arrayWithArray:arry];
        
        for (StampButton* btn in arry) {
            [self addSubview:btn];
        }
    }
}

- (IBAction)stampTap:(id)sender
{
    StampButton *button = sender;
    EBEmotion* emotion = button.data[STAMP_CUSTOM_DATA_EMOTION_NAME];
    UIImage *image = [UIImage imageWithContentsOfFile:emotion.dynamicFilepath];
    image = [image scaleToSize:CGSizeMake(20, 20)];
    [self.textView insertObject:image size:image.size customData:button.data];
//    UIImage *stampImage = [button imageForState:UIControlStateNormal];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
